import json
from decimal import Decimal
from gettext import translation

from django.http import JsonResponse
from django.shortcuts import render
from django.views import View
from django_redis import get_redis_connection

from apps.goods.models import SKU
from apps.orders.models import OrderInfo, OrderGoods
from apps.users.models import Address
from utils.views import LoginRequiredMixin



# Create your views here.
class OrderSettlementView(LoginRequiredMixin,View):

    def get(self, request):
        user = request.user
        addresses = Address.objects.filter(is_deleted=False)
        addresses_list = []
        for address in addresses:
            addresses_list.append({
                'id': address.id,
                'province': address.province.name,
                'city': address.city.name,
                'district': address.district.name,
                'place': address.place,
                'receiver': address.receiver,
                'mobile': address.mobile
            })
        redis_cli = get_redis_connection('carts')
        pipeline = redis_cli.pipeline()
        pipeline.hgetall('carts_%s' % user.id)
        pipeline.smembers('selected_%s' % user.id)
        result = pipeline.execute()
        # result = [hash结果，set结果]
        sku_id_counts = result[0]
        selected_ids = result[1]

        selected_carts = {}
        for sku_id in selected_ids:
            selected_carts[int(sku_id)] = int(sku_id_counts[sku_id])


        sku_list = []
        for sku_id, count in selected_carts.items():
            sku = SKU.objects.get(pk=sku_id)

            sku_list.append({
                'id': sku.id,
                'name': sku.name,
                'count': count,
                'default_image_url': sku.default_image.url,
                'price': sku.price
            })

        from decimal import Decimal
        freight = Decimal('10')

        context = {
            'skus': sku_list,
            'addresses': addresses_list,
            'freight': freight
        }

        return JsonResponse({'code': 0, 'errmsg': 'ok', 'context': context})

class OrderCommitView(View):

    def post(self, request):
        user = request.user
        data = json.loads(request.body.decode())
        address_id = data.get('address_id')
        pay_method = data.get('pay_method')

        if not all([address_id, pay_method]):
            return JsonResponse({'code': 400, 'errmsg': '缺少必传参数'})

        try:
            address = Address.objects.get(id=address_id)
        except Address.DoesNotExist:
            return JsonResponse({'code': 400, 'errmsg': '地址不存在'})

        if pay_method not in [OrderInfo.PAY_METHODS_ENUM['CASH'], OrderInfo.PAY_METHODS_ENUM['ALIPAY']]:
            return JsonResponse({'code': 400, 'errmsg': '支付方式错误'})

        from django.utils import timezone
        order_id = timezone.localtime().strftime('%Y%m%d%H%M%S') + '%09d' % user.id

        if pay_method == OrderInfo.PAY_METHODS_ENUM['CASH']:
            status = OrderInfo.ORDER_STATUS_ENUM['UNSEND']
        else:
            status = OrderInfo.ORDER_STATUS_ENUM['UNPAID']

        total_count = 0
        from decimal import Decimal
        total_amount = Decimal('0')
        freight = Decimal('10')

        from django.db import transaction
        with transaction.atomic():

            point = transaction.savepoint()

            orderinfo = OrderInfo.objects.create(
                order_id=order_id,
                user=user,
                address=address,
                total_count=total_count,
                total_amount=total_amount,
                freight=freight,
                pay_method=pay_method,
                status=status
            )

            redis_cli = get_redis_connection('carts')
            sku_id_counts = redis_cli.hgetall('carts_%s' % user.id)
            selected_ids = redis_cli.smembers('selected_%s' % user.id)

            carts = {}
            for sku_id in selected_ids:
                carts[int(sku_id)] = int(sku_id_counts[sku_id])

            for sku_id, count in carts.items():
                # for i in range(10):
                while True:
                    sku = SKU.objects.get(id=sku_id)
                    if sku.stock < count:
                        transaction.savepoint_rollback(point)
                        return JsonResponse({'code': 400, 'errmsg': '库存不足'})

                    from time import sleep
                    # sleep(7)
                    # sku.stock -= count
                    # sku.sales += count
                    # sku.save()

                    #旧
                    old_stock = sku.stock
                    #新
                    new_stock = sku.stock - count
                    new_sales = sku.sales + count

                    result = SKU.objects.filter(id=sku_id, stock=old_stock).update(stock=new_stock, sales=new_sales)
                    if result == 0:
                        sleep(0.005)
                        continue
                        # transaction.savepoint_rollback(point)
                        # return JsonResponse({'code': 400, 'errmsg': '下单失败~~'})

                    orderinfo.total_count += count
                    orderinfo.total_amount += (sku.price * count)

                    OrderGoods.objects.create(
                        order=orderinfo,
                        sku=sku,
                        count=count,
                        price=sku.price
                    )
                    break
            orderinfo.save()
            transaction.savepoint_commit(point)

        return JsonResponse({'code': 0, 'errmsg': 'ok','order_id': order_id})

